package com.managerplat.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.PersistenceContext;

import org.junit.BeforeClass;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.managerplat.dao.UserDAO;
import com.managerplat.model.OrderInfo;
import com.managerplat.model.UserInfo;
@Repository("ud")
@Transactional
public class UserDAOImpl implements UserDAO{
	@PersistenceContext(name="un")

	private EntityManager em ;
	public int addUser(UserInfo user) {
		// TODO Auto-generated method stub
		
		em.persist(user);
		System.out.println(user.getUserId());
		return user.getUserId();
	}
	@Override
	public int getUserId(String userName) {
		// TODO Auto-generated method stub
		String jpql = "select u from UserInfo u where u.userName=:userName";
		List<UserInfo> list = em.createQuery(jpql).getResultList();
		int id = 0;
		for(UserInfo u:list)
		{
			System.out.println(u.getUserId());
			id = u.getUserId();
		}
		return id;
	}
	@Override
	public int checkUser(String userName, String userPassword) {
		// TODO Auto-generated method stub
		String jpql = "select u from UserInfo u where u.userName=:userName and u.userPassword=:userPassword";
		List<UserInfo> list = em.createQuery(jpql)
				.setParameter("userName", userName)
				.setParameter("userPassword", userPassword)
				.getResultList();
		int id = 0;
		for(UserInfo u:list)
		{
			System.out.println(u.getUserId());
			id = u.getUserId();
		}
		return id;
	}
	public void addOrderInfo(OrderInfo order) {
		// TODO Auto-generated method stub
		em.persist(order);
	}

	@Override
	public List<OrderInfo> findOrders(int sellerId, int isSuc) {
		// TODO Auto-generated method stub
		String jpql = "select o from OrderInfo o where o.sellerId=:sellerId and o.isSuc=:isSuc";
		List<OrderInfo> list = em.createQuery(jpql)
				.setParameter("sellerId", sellerId)
				.setParameter("isSuc", isSuc)
				.getResultList();
		return list;
	}
	

}
